package com.shark.demo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName NumberAndLetter
 * @Description TODO
 * @Author 东北彭于晏
 * @Date 2024/9/29  18:17
 * @Version 1.0
 **/
public class NumberAndLetter {
    public static void main(String[] args) {
        aa a = new aa();
        new Thread(a::a).start();
        new Thread(a::b).start();
    }
}

class aa {
    int number = 1;
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();

    public void a() {
        lock.lock();

        try {
            for (int i = 1; i <= 52; i++) {
                while (number % 3 == 0) {
                    System.out.print("");
                    condition1.await();
                }
                System.out.print(i);
                number++;
                condition2.signal();
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }


    public void b() {
        lock.lock();
        try {
            for (char i = 'A'; i <= 'Z'; i++) {
                while (number % 3 != 0) {
                    try {
                        condition2.await();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print(i + "   ");
                number++;
                condition1.signal();
            }
        } finally {
            lock.unlock();
        }
    }
}